<template>
  <el-dialog v-model="open" :title="title" width="500px" append-to-body :close-on-click-modal="false">
    <el-form label-width="100px">
      <el-form-item v-if="showSites" label="选择场地">
        <el-select
          v-model="form.siteIds"
          style="width: 100%"
          class="mb20"
          multiple
          filterable
          placeholder="选择场地"
        >
          <el-option
            v-for="item in siteList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="节假日模板">
        <el-select
          v-model="form.holidayIds"
          style="width: 100%"
          class="mb20"
          multiple
          filterable
          placeholder="选择节假日"
        >
          <el-option
            v-for="item in canUseHolidys"
            :key="item.id"
            :label="item.holidayName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { getCurrentInstance, reactive } from '@vue/runtime-core'
import { toRefs } from '@vueuse/shared'
import { watch } from 'vue'
import { siteHolidayBind, getsiteHoliday, getSiteList, siteHolidayBindMultiAdd } from '../../../../../../api/ven'

const { proxy } = getCurrentInstance()
const props = defineProps({
  props: {
    type: Object,
    default: () => ({
      open: false,
      title: ''
    })
  },
  baseHolidys: {
    type: Array,
    default: () => []
  }
})
const data = reactive({
  form: {
    siteIds: [],
    holidayIds: [],
    siteId: ''
  },
  showSites: false,
  canUseHolidys: [],
  siteList: []
})

const emit = defineEmits(['submitForm'])
const { open, title } = toRefs(props.props)
const { form, showSites, canUseHolidys, siteList } = toRefs(data)

function submitForm() {
  if (showSites.value) {
    if (form.value.siteIds.length === 0) {
      proxy.$modal.msgWarning('请选择场地')
      return
    }

    siteHolidayBindMultiAdd(form.value).then(() => {
      proxy.$modal.msgSuccess('操作成功')
      open.value = false
      emit('submitForm')
    })
  } else {
    siteHolidayBind(form.value).then(() => {
      proxy.$modal.msgSuccess('操作成功')
      open.value = false
      emit('submitForm')
    })
  }
}

watch(open, (val) => {
  if (val) {
    form.value = {
      siteIds: [],
      holidayIds: [],
      siteId: ''
    }

    // 筛选启用状态的节假日
    canUseHolidys.value = _.filter(props.baseHolidys, ['status', '1'])

    if (props.props.data.id) {
      showSites.value = false
      // 获取holidayIds
      form.value.siteId = props.props.data.id
      getsiteHoliday({ siteId: props.props.data.id }).then(res => {
        const hid = []
        for (var n of res.rows) {
          hid.push(n.holidayPriceId)
        }
        form.value.holidayIds = hid
      })
    } else {
      showSites.value = true

      if (siteList.value.length === 0) {
        getSiteList().then((res) => {
          siteList.value = res.rows
        })
      }
    }
  }
})


</script>

<style scoped>

</style>
